package com.easy.component.ibase;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.easy.component.R;
import com.easy.component.dialog.EasyAlertDialog;
import com.easy.component.dialog.EasyLoadingDialog;
import com.easy.component.subscription.EasyBaseSubscription;
import com.easy.component.utils.statusbar.StatusBarUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * 描述: {描述}
 * 作者:jack-song
 * 创建日期: 2017/11/2 10:29
 * 修改日期: 2017/11/2
 * 版本: {版本}
 */
public abstract class EasyBaseActivity extends Activity implements EasyBaseActivityController {
    /**
     * loading
     */
    private EasyLoadingDialog loadingDialog;
    /**
     * alertDialog
     */
    private EasyAlertDialog alertDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        init();
    }

    protected void init() {
        initlayoutResID();
        initBundle();
        initView();
        initHeader();
        initLoadingView();
        initData();
    }

    public void goActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }

        this.startActivity(intent);
    }

    public void goActivity(Class<?> cls) {
        this.goActivity(cls, (Bundle) null);
    }

    protected void goActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
        Intent intent = new Intent(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }

        this.startActivityForResult(intent, requestCode);
    }

    protected void goActivityForResult(Class<?> cls, int requestCode) {
        this.goActivityForResult(cls, (Bundle) null, requestCode);
    }

    protected Bundle $getIntentExtra() {
        Intent intent = this.getIntent();
        Bundle bundle = null;
        if (null != intent) {
            bundle = intent.getExtras();
        }

        return bundle;
    }

    @Subscribe
    public void onEventMainThread(EasyBaseSubscription event) {
    }

    public void finishActivity() {
        super.finish();
    }

    /**
     * 显示loading
     * dialog 可以取消
     * activity不跟随返回键关闭
     */
    public void showLoading() {
        showLoading(getString(R.string.easy_loading), true, false);
    }

    /**
     * 显示loading
     * dialog 可以取消
     * activity不跟随返回键关闭
     *
     * @param message
     */
    public void showLoading(CharSequence message) {
        showLoading(message, true, false);
    }

    /**
     * 显示loading
     *
     * @param isCancelable 是否可以取消
     */
    public void showLoading(boolean isCancelable) {
        showLoading(getString(R.string.easy_loading), isCancelable, false);
    }

    /**
     * 显示loading
     * dialog 不可以取消
     *
     * @param message
     */
    public void showLoading(CharSequence message, boolean isCancelable) {
        showLoading(message, isCancelable, isCancelable);
    }

    /**
     * 显示loading
     * 关闭Activtiy
     */
    public void showLoadingCloseActivity() {
        showLoading(getString(R.string.easy_loading), false, true);
    }

    /**
     * @param message
     */
    public void showLoadingCloseActivity(CharSequence message) {
        showLoading(message, false, true);
    }

    /**
     * 显示loading
     *
     * @param message
     * @param isCancelable
     * @param isCloseActivity
     */
    public void showLoading(CharSequence message, boolean isCancelable, boolean isCloseActivity) {
        hideLoading();
        loadingDialog = new EasyLoadingDialog.Builder().setContext((this.getParent() == null) ? this : this.getParent())
                .setMessage(message).setCancelable(isCancelable).setCloseActivity(isCloseActivity).setOnBackPressedListener(new EasyLoadingDialog.OnBackPressedListener() {
                    @Override
                    public void onBackPressed() {
                        finishActivity();
                    }
                }).builder();
        loadingDialog.show();
    }

    /**
     * 隐藏loading
     */
    public void hideLoading() {
        if (null != loadingDialog && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
        if (null != loadingDialog) {
            loadingDialog = null;
        }
    }

    /**
     * @param title
     * @param message
     * @param leftOnClickListener
     */
    public void showAlert(CharSequence title, CharSequence message, View.OnClickListener leftOnClickListener) {
        showAlert(title, message, null, leftOnClickListener, null, null);
    }

    /**
     * 显示alert 样式的弹出框
     *
     * @param message
     * @param leftOnClickListener
     * @param rigthOnClickListener
     */
    public void showAlert(CharSequence message, View.OnClickListener leftOnClickListener, View.OnClickListener rigthOnClickListener) {
        showAlert(null, message, null, leftOnClickListener, null, rigthOnClickListener);
    }

    /**
     * @param title
     * @param message
     * @param leftOnClickListener
     * @param rigthOnClickListener
     */
    public void showAlert(CharSequence title, CharSequence message, View.OnClickListener leftOnClickListener, View.OnClickListener rigthOnClickListener) {
        showAlert(title, message, null, leftOnClickListener, null, rigthOnClickListener);
    }

    /**
     * 显示alert 样式的弹出框
     *
     * @param title
     * @param message
     * @param leftText
     * @param leftOnClickListener
     * @param rigthText
     * @param rigthOnClickListener
     */
    public void showAlert(CharSequence title, CharSequence message, CharSequence leftText, View.OnClickListener leftOnClickListener,
                          CharSequence rigthText, View.OnClickListener rigthOnClickListener) {
        alertDialog = new EasyAlertDialog.Builder().setContext(this).setTitle(title).setMessage(message)
                .setLeftText(leftText).setLeftOnClickListener(leftOnClickListener).setRigthText(rigthText)
                .setRigthOnClickListener(rigthOnClickListener).builder();
        alertDialog.show();
    }

    /**
     * 关闭alert 样式的弹出框
     */
    public void hideAlert() {
        if (null != alertDialog && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
        if (null != alertDialog) {
            alertDialog = null;
        }
    }

    /**
     * 设置头部沉浸式状态栏
     *
     * @param statusBarColor 状态栏颜色 如不设置传0 默认为页面背景色，
     * @param isDarkTheme    设置字体是否为深色
     * @param isStatusPic    设置布局是否顶进状态栏 （一般为图片时使用）
     *                       （如图片不需进入状态栏 且仍要沉浸时，要在布局中加入StatusBarHeightView）
     */
    public void setStatusTheme(int statusBarColor, boolean isDarkTheme, boolean isStatusPic) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //当FitsSystemWindows设置 true 时，会在屏幕最上方预留出状态栏高度的 padding
            StatusBarUtil.setRootViewFitsSystemWindows(this, !isStatusPic);
            //设置状态栏透明
            StatusBarUtil.setTranslucentStatus(this);
            if (statusBarColor != 0) {
                StatusBarUtil.setStatusBarColor(this, ContextCompat.getColor(this, statusBarColor));
            }

            if (isDarkTheme) {
                //设置深色文字图标风格
                if (!StatusBarUtil.setStatusBarDarkTheme(this, true)) {
                    //如果不支持设置深色风格 设置一个状态栏颜色为半透明, 这样半透明+白=灰, 状态栏的文字能看得清
                    StatusBarUtil.setStatusBarColor(this, 0x55000000);
                }
            } else {
                StatusBarUtil.setStatusBarDarkTheme(this, false);
            }
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        hideLoading();
    }
}
